package com.netrust.cmsx.admin.modules.sys.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.netrust.cmsx.admin.modules.sys.service.RoleService;
import com.netrust.cmsx.admin.modules.sys.service.UserService;
import com.netrust.cmsx.core.config.Global;
import com.netrust.cmsx.core.page.PageKey;
import com.netrust.cmsx.core.page.jq.JqGrid;
import com.netrust.cmsx.core.page.jq.JqPost;
import com.netrust.cmsx.core.page.jq.convert.Page2JQ;
import com.netrust.cmsx.core.resp.RespJson;
import com.netrust.cmsx.core.validator.BeanValidators;
import com.netrust.cmsx.core.web.BaseController;
import com.netrust.cmsx.entity.module.sys.Role;
import com.netrust.cmsx.entity.module.sys.User;
import com.netrust.cmsx.utils.data.DateUtils;
import com.netrust.cmsx.utils.string.StringUtils;


/**
 * 用户Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	
	@ResponseBody
	@RequestMapping(value = {""})
	public User get(@RequestParam(required=false) String id) {
		if (id!=null){
			return userService.findOne(id);
		}else{
			return new User();
		}
	}
	
	@RequestMapping(value = {"form"})
	public String form(@RequestParam(required=false) String id,Model model) {
		List<Role> roles=roleService.findRoleEnable();
		User user=new User();
		if(id!=null){//代表修改跳转
			user=userService.findOne(id);			
			List<String> roleIds=user.getRoleIdList();
			for (int i = 0; i < roles.size(); i++) {
				if(roleIds.contains(roles.get(i).getId())){
					roles.get(i).setChecked(true);
				}
			}
		}
		
		model.addAttribute("user", user);
		model.addAttribute("allRoles", roles);
		return "modules/sys/userForm";
	}
	
	@RequestMapping(value = {"save"})
	public String save(User user) {		
		try {
			userService.saveUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "modules/sys/userList";
	}
	
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = {"del"})
	public RespJson del(@RequestParam(required=true) String id) {
		try {
			userService.deleteInFlag(new User(id));
			return new RespJson(Global.Operate_Success, 200);
		} catch (Exception e) {
			return new RespJson(Global.Operate_Error, 500, null);
		}
	}

	@ResponseBody
	@RequestMapping(value = {"list"})
	public JqGrid list(JqPost jqPost) {
		PageKey pk=new PageKey(null, jqPost.getPage(),jqPost.getRows());
		Page<User> page =userService.findList(pk);
		JqGrid jg=new Page2JQ<User>().run(page,new String[]{"loginName","email","phone","mobile","loginDate","remarks"});		
		return jg;
	}
}
